package toanstt.com.gameplay;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import toanstt.com.MatchUp.MainActivity;
import toanstt.com.MatchUp.Notice;
import toanstt.com.MatchUp.Sound2;
import toanstt.com.MatchUp.T;
import toanstt.com.MatchUp.TBitmap2;
import toanstt.com.MatchUp.Vec2;

public class CellTable 
{
	Paint paint_white;
	int temp_x,temp_y;
	int action, actionCode;
	int x,y;
	int selectx = -1,selecty =-1;
	TBitmap2 CARDS;
	TBitmap2 MAGIC;
	static int CARDINDEX[][];
	static int CARD_SLIP_INDEX[][];
	Bitmap CARD_SLIPS[][];
	int slipwidth[];
	int delay=0;
	public static  int COUNT_DONE;
	public CellTable()
	{
		paint_white = new Paint();
		paint_white.setColor(Color.BLACK);
		
	}
	
	public void NewGame(int i1,int i2)
	{
		T.GAME_COL =4;
		T.GAME_ROW =4;
		GameStatus.LEV = "LEVEL "+(i1+1)+"."+i2;
		switch(i1)
		{
			case 0:
				T.GAME_COL =4;
				T.GAME_ROW =4;
				//GameStatus.LEV = "EASY";
			break;
			case 1:
				T.GAME_COL =5;
				T.GAME_ROW =4;
				//GameStatus.LEV = "NORMAL";
			break;
			case 2:
				T.GAME_COL =6;
				T.GAME_ROW =5;
				//GameStatus.LEV = "HARD";
			break;
			case 3:
				T.GAME_COL =6;
				T.GAME_ROW =6;
				//GameStatus.LEV = "EXTREME";
			break;
		}

		
		T.CELL_WIDTH = (T.DISPLAY_WIDTH -20)/T.GAME_COL;
		T.GAME_MINY =(T.DISPLAY_HEIGHT-50)/2 - T.GAME_ROW*T.CELL_WIDTH/2;
		T.GAME_MINY = (int)(T.GAME_MINY*0.8f);
		
		T.GAME_HEIGHT =T.GAME_ROW* T.CELL_WIDTH;
		T.GAME_WIDTH=T.GAME_COL* T.CELL_WIDTH;
		T.GAME_MINX = T.DISPLAY_WIDTH/2 -T.GAME_WIDTH/2;
		
		
		CARDS = new TBitmap2("card/cards4x4.png", 4, 4, T.CELL_WIDTH);
		
		
		
		CARD_SLIPS = new Bitmap[5][16];
		RandomCARDINDEX();
		slipwidth = new int[5];
		slipwidth[0] = (int)(T.CELL_WIDTH*0.70f);
		slipwidth[1] = (int)(T.CELL_WIDTH*0.45f);
		slipwidth[2] = (int)(T.CELL_WIDTH*0.25f);
		slipwidth[3] = (int)(T.CELL_WIDTH*0.12f);
		slipwidth[4] = (int)(T.CELL_WIDTH*0.05f);
		for(int i=0;i<5;i++)
		{
			for(int j=0;j<16;j++)
			{
				CARD_SLIPS[i][j] = Bitmap.createScaledBitmap(CARDS.bitmap[j], slipwidth[i], T.CELL_WIDTH, true);
			}
		}
		CARD_SLIP_INDEX = new int[T.GAME_ROW][];
		for(int i =0;i<T.GAME_ROW;i++)
			CARD_SLIP_INDEX[i] = new int[T.GAME_COL];
		
		MAGIC = new TBitmap2("magic.png",1,5,T.CELL_WIDTH);
		
		T.STATUS_MINX = T.GAME_MINX + T.GAME_WIDTH;
		T.STATUS_MINY = 20;
		COUNT_DONE =0;
	}
	int n;
	void RandomCARDINDEX()
	{
		 n= T.GAME_ROW*T.GAME_COL;
		int c[];
		c = new int[n];
		for(int i =0;i<n/2;i++)
			c[i] = T.random.nextInt(10);
			//c[i] = 0;
		for(int i =n/2;i<n;i++)
			c[i] = c[i-n/2];
		int index1;
		int index2;
		int temp;
		for(int i =0;i<n;i++)
		{
			index1 = T.random.nextInt(n);
			index2 = T.random.nextInt(n);
			temp = c[index1];
			c[index1] = c[index2];
			c[index2] = temp;
		}
		temp =0;
		CARDINDEX = new int[T.GAME_ROW][];
		for(int i =0;i<T.GAME_ROW;i++)
		{
			CARDINDEX[i] = new int[T.GAME_COL];
			for(int j =0;j<T.GAME_COL;j++)
			{
				CARDINDEX[i][j] =c[temp];
				temp++;
			}
		}
	}
	int i,j;
	int i2,i3,j3;
	public void Draw(Canvas canvas)
	{
		//canvas.drawRect(T.GAME_MINX, T.GAME_MINY, T.GAME_MINX + T.GAME_WIDTH, T.GAME_MINY + T.GAME_HEIGHT, paint_white);
		
		
		for(i =0;i<T.GAME_ROW;i++)
		{
			for(j =0;j<T.GAME_COL;j++)
			{
				if(CARD_SLIP_INDEX[i][j] ==-1)
					continue;
				if(CARD_SLIP_INDEX[i][j] ==0)
				{
					//canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					canvas.drawBitmap(CARDS.bitmap[15], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
				}
				else if(CARD_SLIP_INDEX[i][j] <9)
				{
					
					if(CARD_SLIP_INDEX[i][j]<5)
					{
						//if(CARD_SLIP_INDEX[i][j] ==2)
							//T.sounds.Play(Sound2.SLIPUP);
						i2 = CARD_SLIP_INDEX[i][j]-1;
						canvas.drawBitmap(CARD_SLIPS[i2][15], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
					}
					else 
					{
						i2 =  3 - (CARD_SLIP_INDEX[i][j]-5);
						canvas.drawBitmap(CARD_SLIPS[i2][CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
					}
					CARD_SLIP_INDEX[i][j]++;
				}
				else if(CARD_SLIP_INDEX[i][j] ==9)
				{
					canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					if(GameStatus.INHINT ==1)
					{
						CARD_SLIP_INDEX[i][j] =12;
					}
					else
					if(GameStatus.INHINT ==0)
					{
						for(i3 =0;i3<T.GAME_ROW;i3++)
						{
							for(j3 =0;j3<T.GAME_COL;j3++)
							{
								if(CARD_SLIP_INDEX[i3][j3] == 9 && (i3!=i || j3!=j))
								{
									if(CARDINDEX[i][j] == CARDINDEX[i3][j3])
									{
										T.sounds.Play(Sound2.RIGHT);
										CARD_SLIP_INDEX[i3][j3] =26;
										CARD_SLIP_INDEX[i][j] =26;
										//CARDINDEX[i3][j3] =-1;
										//CARDINDEX[i][j] =-1;
										
										COUNT_DONE+=2;
										if(COUNT_DONE ==n)
										{
											
											T.sounds.Play(Sound2.WIN);
											if(GameStatus.FAIL < T.TIME_BOUNS[T.GAME_I1])
											{
												T.notice.DoNotice(Notice.FINISH, 3);
												T.GAME_DATA[T.GAME_I1][T.GAME_I2] = 3;
											}
											else if(GameStatus.FAIL < (int)(1.4f*T.TIME_BOUNS[T.GAME_I1]))
											{
												T.notice.DoNotice(Notice.FINISH, 2);
												T.GAME_DATA[T.GAME_I1][T.GAME_I2] = 2;
											}
											else
											{
												T.notice.DoNotice(Notice.FINISH, 1);
												T.GAME_DATA[T.GAME_I1][T.GAME_I2] = 1;
											}
											
										}
									}
									else
									{
										T.sounds.Play(Sound2.FAIL);
										
										CARD_SLIP_INDEX[i3][j3] =12;
										CARD_SLIP_INDEX[i][j] =12;
										GameStatus.FAIL++;
									}
								}
							}
						}
					}
					
				}
				else if(CARD_SLIP_INDEX[i][j] <26 )
				{
					if(CARD_SLIP_INDEX[i][j]<18)
						canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					else if(CARD_SLIP_INDEX[i][j]<21)
					{
						i2 = CARD_SLIP_INDEX[i][j]-18;
						canvas.drawBitmap(CARD_SLIPS[i2][CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
						
					}
					else if(CARD_SLIP_INDEX[i][j]<26)
					{
						i2 =  3 - (CARD_SLIP_INDEX[i][j]-22);
						canvas.drawBitmap(CARD_SLIPS[i2][15], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
					}
					CARD_SLIP_INDEX[i][j]++;
					if(CARD_SLIP_INDEX[i][j] >=26)
					{
						CARD_SLIP_INDEX[i][j] =0;
						
					}
				}
				else if(CARD_SLIP_INDEX[i][j] >=26 && CARD_SLIP_INDEX[i][j] <31 )
				{
					T.paint_alpha.setAlpha(255/((CARD_SLIP_INDEX[i][j]-25)));
					
					canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, T.paint_alpha);
					canvas.drawBitmap(MAGIC.bitmap[CARD_SLIP_INDEX[i][j]-26], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					CARD_SLIP_INDEX[i][j]++;
					if(CARD_SLIP_INDEX[i][j]==31)
						CARD_SLIP_INDEX[i][j] =-1;
				}
				/*if(CARD_SLIP_INDEX[i][j] ==-1)
					continue;
				if(CARD_SLIP_INDEX[i][j] ==0)
				{
					//canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					canvas.drawBitmap(CARDS.bitmap[15], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
				}
				else if(CARD_SLIP_INDEX[i][j] <11)
				{
					
					if(CARD_SLIP_INDEX[i][j]<6)
					{
						i2 = CARD_SLIP_INDEX[i][j]-1;
						canvas.drawBitmap(CARD_SLIPS[i2][15], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
					}
					else 
					{
						i2 =  4 - (CARD_SLIP_INDEX[i][j]-6);
						canvas.drawBitmap(CARD_SLIPS[i2][CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
					}
					CARD_SLIP_INDEX[i][j]++;
				}
				else if(CARD_SLIP_INDEX[i][j] ==11)
				{
					canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					if(GameStatus.INHINT ==1)
					{
						CARD_SLIP_INDEX[i][j] =12;
					}
					else
					if(GameStatus.INHINT ==0)
					{
						for(i3 =0;i3<T.GAME_ROW;i3++)
						{
							for(j3 =0;j3<T.GAME_COL;j3++)
							{
								if(CARD_SLIP_INDEX[i3][j3] == 11 && (i3!=i || j3!=j))
								{
									if(CARDINDEX[i][j] == CARDINDEX[i3][j3])
									{
										CARD_SLIP_INDEX[i3][j3] =40;
										CARD_SLIP_INDEX[i][j] =40;
										//CARDINDEX[i3][j3] =-1;
										//CARDINDEX[i][j] =-1;
										
										COUNT_DONE+=2;
										if(COUNT_DONE ==n)
										{
											if(GameStatus.FAIL < T.TIME_BOUNS[T.GAME_I1])
											{
												T.notice.DoNotice(Notice.FINISH, 3);
												T.GAME_DATA[T.GAME_I1][T.GAME_I2] = 3;
											}
											else if(GameStatus.FAIL < (int)(1.5f*T.TIME_BOUNS[T.GAME_I1]))
											{
												T.notice.DoNotice(Notice.FINISH, 2);
												T.GAME_DATA[T.GAME_I1][T.GAME_I2] = 2;
											}
											else
											{
												T.notice.DoNotice(Notice.FINISH, 1);
												T.GAME_DATA[T.GAME_I1][T.GAME_I2] = 1;
											}
										}
									}
									else
									{
										CARD_SLIP_INDEX[i3][j3] =12;
										CARD_SLIP_INDEX[i][j] =12;
										GameStatus.FAIL++;
									}
								}
							}
						}
					}
					
				}
				else if(CARD_SLIP_INDEX[i][j] <30 )
				{
					if(CARD_SLIP_INDEX[i][j]<20)
						canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					else if(CARD_SLIP_INDEX[i][j]<25)
					{
						i2 = CARD_SLIP_INDEX[i][j]-20;
						canvas.drawBitmap(CARD_SLIPS[i2][CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
						
					}
					else if(CARD_SLIP_INDEX[i][j]<30)
					{
						i2 =  4 - (CARD_SLIP_INDEX[i][j]-25);
						canvas.drawBitmap(CARD_SLIPS[i2][15], T.GAME_MINX + j*T.CELL_WIDTH +T.CELL_WIDTH /2 -  slipwidth[i2]/2, T.GAME_MINY + i*T.CELL_WIDTH, null);
					}
					CARD_SLIP_INDEX[i][j]++;
					if(CARD_SLIP_INDEX[i][j] >=30)
					{
						CARD_SLIP_INDEX[i][j] =0;
						
					}
				}
				else if(CARD_SLIP_INDEX[i][j] >=40 && CARD_SLIP_INDEX[i][j] <50 )
				{
					T.paint_alpha.setAlpha(255/(5-(CARD_SLIP_INDEX[i][j]-40)));
					
					canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, T.paint_alpha);
					canvas.drawBitmap(MAGIC.bitmap[CARD_SLIP_INDEX[i][j]-40], T.GAME_MINX + j*T.CELL_WIDTH, T.GAME_MINY + i*T.CELL_WIDTH, null);
					CARD_SLIP_INDEX[i][j]++;
					if(CARD_SLIP_INDEX[i][j]==45)
						CARD_SLIP_INDEX[i][j] =-1;
				}*/
			}
		}
	}
	int dx,dy;
	int count;
	int i5,j5;
	public void touch(MotionEvent mo)
	{
		
		action = mo.getAction();
		actionCode = action & MotionEvent.ACTION_MASK;
		temp_x = (int)mo.getX();
		temp_y = (int)mo.getY();
		dx = temp_x-T.GAME_MINX;
		
		if(action == MotionEvent.ACTION_DOWN)
		if(dx>=0 && dx <=T.GAME_WIDTH)
		{
			dy = temp_y-T.GAME_MINY;
			if(dx>=0 && dy <=T.GAME_HEIGHT)
			{
				dx = dx/T.CELL_WIDTH;
				dy = dy/T.CELL_WIDTH;
				T.sounds.Play(T.sounds.BUTTON_CLICK);
				if(dx < 0 || dy < 0)
					return;
				if(CARD_SLIP_INDEX[dy][dx] ==9)
				{
					if(GameStatus.INHINT ==0)
						CARD_SLIP_INDEX[dy][dx] =20;
				}
				else
				if(CARD_SLIP_INDEX[dy][dx] ==0)
				{
					count =0;
					for(i5 =0;i5<T.GAME_ROW;i5++)
					{
						for(j5 =0;j5<T.GAME_COL;j5++)
						{		
							if(CARD_SLIP_INDEX[i5][j5]>0)
								count++;
						}
					}
					if(count <3)
						CARD_SLIP_INDEX[dy][dx] =1;
				}
				
					
			}
		}
	}
	void LoadCards()
	{
		
	}
}
